package edu.ynmd.cms.tools;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;

import java.io.File;

public class OssTools {
    private static COSCredentials cred;
    private static ClientConfig clientConfig;
    private static COSClient cosClient;
    private static String bucketName ="mybucket-1251668577";

    public void init(){
        cred = new BasicCOSCredentials("AKIDVouofMcTQ8i4HJpsNddS2QaVIq8Axe4E", "32mMl5C4kMo59yc1DB2lIadpmUwAE4g6");
// 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
// clientConfig中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        clientConfig = new ClientConfig(new Region("ap-chengdu"));
// 3 生成 cos 客户端。
        cosClient= new COSClient(cred, clientConfig);
    }

    public String uploadFileForUrl(String filename,File touploadfile) throws Exception{
        String bucketName = "cms-1256615213";
        String key = filename;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, touploadfile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);

        return putObjectResult.getRequestId();
    }
}

